<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 20:31
 */

namespace app\storage\model;


use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;


class PurchaseDetailModel extends Model
{
    protected $table = 'iwh_purchase_detail';
    protected $pk = 'uuid';


    /**
     * @param $condition
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getPurchaseDetail($condition)
    {
        $where = $condition['where'];
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        $result['total'] = $this->where($where)->count();
        $result['row'] = $this
            ->alias('p')
            ->where($where)
            ->order('time desc')
            ->join(['iwh_supplier' => 's'], 's.supplier_id = p.supplier_id', 'left')
            ->page($pageIndex, $pageSize)
            ->select();
        return $result;
    }

    public function queryDetail($condition){
        $where = $condition['where'];
        $pageSize = $condition['pageSize'];
        $pageIndex = $condition['pageIndex'];
        if(!empty($where['supplier_id'])){
            $where['p.supplier_id'] = $where['supplier_id'];
            unset($where['supplier_id']);
        }
        $result['total'] = $this->alias('p')->field('p.*,s.company_name')
            ->join(['iwh_supplier' => 's'], 's.supplier_id = p.supplier_id', 'left')
            ->where($where)
            ->count();

        $result['row'] = $this->alias('p')->field('p.*,s.company_name')
            ->join(['iwh_supplier' => 's'], 's.supplier_id = p.supplier_id', 'left')
            ->page($pageIndex, $pageSize)
            ->order('time desc')
            ->where($where)
            ->select();
        return $result;
    }


    /**
     * 打印标签时查询reelId
     */
    public function getReelIdData($where)
    {
        $result = $this->where($where)->count();
        return $result;
    }


    /**查询reelId信息
     * @param $reelId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function findReelInfo($reelId)
    {
        $where['reel_id'] = ['like', $reelId];
        $result = $this->alias('Pd')
            ->join(['iwh_sub_materiel' => 'Sm'], 'Sm.sub_materiel_id = Pd.sub_materiel_id')
            ->field('Pd.sub_materiel_id,reel_id,count,format,sub_materiel_name')
            ->where($where)
            ->order('reel_id desc')
            ->find();
        return $result;
    }

    /**
     * 复制标签插入记录时查询reelId信息
     * @param $originalReelId
     * @return array|false|\PDOStatement|string|Model
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getReelInfo($originalReelId)
    {

        $result = $this->where(['reel_id' => $originalReelId])->find();
        return $result;
    }

}